﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace MediaPlayerFieldType {
    class MediaPlayerFieldControl : BaseFieldControl {
        protected override string DefaultTemplateName {
            get {
                return "MediaPlayerTemplate";
            }
        }

        protected override void RenderFieldForDisplay(System.Web.UI.HtmlTextWriter output) {
            if (ListItem.File != null) {
                string playerID = new Guid().ToString();
                switch (Path.GetExtension(ListItem.File.Name).ToLower()) {
                    case ".mov":
                    case ".mpg":
                    case ".mpeg":
                        output.Write(@"<object CLASSID=""clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"" width=""{0}"" height=""{1}"" CODEBASE=""http://www.apple.com/qtactivex/qtplugin.cab"">
<param name=""src"" value=""{2}"">
<param name=""qtsrc"" value=""{2}"">
<param name=""autoplay"" value=""false"">
<param name=""loop"" value=""false"">
<param name=""controller"" value=""true"">
<embed src=""{2}"" qtsrc=""{2}"" width=""{0}"" height=""{1}"" autoplay=""true"" loop=""false"" controller=""true"" pluginspage=""http://www.apple.com/quicktime/""></embed>
</object>", Convert.ToInt32(Field.GetCustomProperty("PlayerWidth")), Convert.ToInt32(Field.GetCustomProperty("PlayerHeight")),
          ListItem.File.ServerRelativeUrl);
                        break;
                    case ".avi":
                    case ".wmv":
                        switch (Convert.ToString(Field.GetCustomProperty("PlayerChoice"))) {
                            case "Windows Media Player":
                                output.Write(@"<OBJECT id=""VIDEO"" width=""{0}"" height=""{1}"" 
	CLASSID=""CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"" type=""application/x-oleobject"">
	<PARAM NAME=""URL"" VALUE=""{2}"">
	<PARAM NAME=""SendPlayStateChangeEvents"" VALUE=""True"">
	<PARAM NAME=""AutoStart"" VALUE=""True"">
	<PARAM name=""uiMode"" value=""mini"">
	<PARAM name=""PlayCount"" value=""9999"">
</OBJECT>", Convert.ToInt32(Field.GetCustomProperty("PlayerWidth")), Convert.ToInt32(Field.GetCustomProperty("PlayerHeight")),
          ListItem.File.ServerRelativeUrl);
                                break;
                            case "Silverlight Player":
                                output.Write(@"<script src=""/resources/silverlight/MicrosoftAjax.js""></script>
<script src=""/resources/silverlight/MicrosoftAjaxWebForms.js""></script>
<script src=""/resources/silverlight/SilverlightControl.js""></script>
<script src=""/resources/silverlight/SilverlightMedia.js""></script>");
                                output.Write(@"<div id='{2}'></div><script type=""text/javascript"">
//<![CDATA[
Sys.UI.Silverlight.Control.createObject(""{2}"", ""\u003cobject type=\""application/x-silverlight\"" data=\""data:application/x-silverlight,\"" id=\""MediaPlayer1\"" style=\""height:{1};width:{0};\""\u003e\r\n\t\u003ca href=\""http://go.microsoft.com/fwlink/?LinkID=108182\""\u003e\u003cimg src=\""http://go.microsoft.com/fwlink/?LinkID=108181\"" alt=\""Get Microsoft Silverlight\"" style=\""border-width:0;\"" /\u003e\u003c/a\u003e\r\n\u003c/object\u003e"");
//]]>
</script>
<script type=""text/javascript"">
//<![CDATA[
Sys.Application.initialize();
Sys.Application.add_init(function() {{
    $create(Sys.UI.Silverlight.MediaPlayer, {{""autoPlay"":true,""mediaSource"":""{3}"",""scaleMode"":1,""source"":""/resources/silverlight/player.xml""}}, null, null, $get(""{2}""));
}});
//]]>
</script>", Convert.ToInt32(Field.GetCustomProperty("PlayerWidth")), Convert.ToInt32(Field.GetCustomProperty("PlayerHeight")),
          playerID, ListItem.File.ServerRelativeUrl);
                                break;
                        }
                        break;
                    case ".flv":
                        output.Write(@"<div id='{3}'><a href=""http://www.macromedia.com/go/getflashplayer"">Get the Flash Player</a> to see this player.</div>
<script type=""text/javascript"" src=""/resources/swfobject.js""></script>
<script type=""text/javascript"">
	var s1 = new SWFObject(""/resources/mediaplayer.swf"",""mediaplayer"",""{0}"",""{1}"",""7"");
	s1.addParam(""allowfullscreen"",""true"");
	s1.addVariable(""width"",""{0}"");
	s1.addVariable(""height"",""{1}"");
	s1.addVariable(""file"",""{2}"");
	s1.addVariable(""image"",""video.jpg"");
	s1.write(""{3}"");
</script>", Convert.ToInt32(Field.GetCustomProperty("PlayerWidth")), Convert.ToInt32(Field.GetCustomProperty("PlayerHeight")),
          ListItem.File.ServerRelativeUrl, playerID);
                        break;
                }
            }
        }
    }
}
